package com.wyd.component.netty.handler;

import com.wyd.component.netty.constants.Constants;
import com.wyd.component.netty.manager.ChannelManager;
import com.wyd.component.netty.message.DeviceBaseMessage;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class BizHandler extends SimpleChannelInboundHandler<DeviceBaseMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, DeviceBaseMessage deviceBaseMessage) throws Exception {

        log.info("==============>channelRead:{}", deviceBaseMessage.toString());
        ChannelManager.handleChannelMsg(ctx.channel(), deviceBaseMessage);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        super.channelActive(ctx);
        log.info("================有新的连接=================");
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        super.channelInactive(ctx);
        if (ctx.channel().attr(Constants.attriKey).get() != null) {
            ChannelManager.removeChannel(ctx.channel().attr(Constants.attriKey).get().getCardId()
            );
        }
    }
}
